// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.characters.CharacterTimedActions;

import se.krka.kahlua.vm.KahluaTable;
import zombie.Lua.LuaManager;
import zombie.ai.astar.IPathfinder;
import zombie.ai.astar.Mover;
import zombie.ai.astar.Path;
import zombie.characters.IsoGameCharacter;
import zombie.core.math.PZMath;
import zombie.core.skinnedmodel.advancedanimation.AnimEvent;

public final class LuaTimedActionNew extends BaseAction implements IPathfinder {
    KahluaTable table;

    public LuaTimedActionNew(KahluaTable kahluaTable, IsoGameCharacter isoGameCharacter) {
        super(isoGameCharacter);
        this.table = kahluaTable;
        Object object0 = kahluaTable.rawget("maxTime");
        this.MaxTime = (Integer)LuaManager.converterManager.fromLuaToJava(object0, Integer.class);
        Object object1 = kahluaTable.rawget("stopOnWalk");
        Object object2 = kahluaTable.rawget("stopOnRun");
        Object object3 = kahluaTable.rawget("stopOnAim");
        Object object4 = kahluaTable.rawget("caloriesModifier");
        Object object5 = kahluaTable.rawget("useProgressBar");
        Object object6 = kahluaTable.rawget("forceProgressBar");
        Object object7 = kahluaTable.rawget("loopedAction");
        if (object1 != null) {
            this.StopOnWalk = (Boolean)LuaManager.converterManager.fromLuaToJava(object1, Boolean.class);
        }

        if (object2 != null) {
            this.StopOnRun = (Boolean)LuaManager.converterManager.fromLuaToJava(object2, Boolean.class);
        }

        if (object3 != null) {
            this.StopOnAim = (Boolean)LuaManager.converterManager.fromLuaToJava(object3, Boolean.class);
        }

        if (object4 != null) {
            this.caloriesModifier = (Float)LuaManager.converterManager.fromLuaToJava(object4, Float.class);
        }

        if (object5 != null) {
            this.UseProgressBar = (Boolean)LuaManager.converterManager.fromLuaToJava(object5, Boolean.class);
        }

        if (object6 != null) {
            this.ForceProgressBar = (Boolean)LuaManager.converterManager.fromLuaToJava(object6, Boolean.class);
        }

        if (object7 != null) {
            this.loopAction = (Boolean)LuaManager.converterManager.fromLuaToJava(object7, Boolean.class);
        }
    }

    @Override
    public void waitToStart() {
        Boolean _boolean = LuaManager.caller.protectedCallBoolean(LuaManager.thread, this.table.rawget("waitToStart"), this.table);
        if (_boolean == Boolean.FALSE) {
            super.waitToStart();
        }
    }

    @Override
    public void update() {
        super.update();
        LuaManager.caller.pcallvoid(LuaManager.thread, this.table.rawget("update"), this.table);
    }

    @Override
    public boolean valid() {
        Object[] object = LuaManager.caller.pcall(LuaManager.thread, this.table.rawget("isValid"), this.table);
        return object.length > 1 && object[1] instanceof Boolean && (Boolean)object[1];
    }

    @Override
    public void start() {
        super.start();
        this.CurrentTime = 0.0F;
        LuaManager.caller.pcall(LuaManager.thread, this.table.rawget("start"), this.table);
    }

    @Override
    public void stop() {
        super.stop();
        LuaManager.caller.pcall(LuaManager.thread, this.table.rawget("stop"), this.table);
    }

    @Override
    public void perform() {
        super.perform();
        LuaManager.caller.pcall(LuaManager.thread, this.table.rawget("perform"), this.table);
    }

    @Override
    public void Failed(Mover mover) {
        this.table.rawset("path", null);
        LuaManager.caller.pcallvoid(LuaManager.thread, this.table.rawget("failedPathfind"), this.table);
    }

    @Override
    public void Succeeded(Path path, Mover mover) {
        this.table.rawset("path", path);
        LuaManager.caller.pcallvoid(LuaManager.thread, this.table.rawget("succeededPathfind"), this.table);
    }

    public void Pathfind(IsoGameCharacter chr, int x, int y, int z) {
    }

    @Override
    public String getName() {
        return "timedActionPathfind";
    }

    public void setCurrentTime(float time) {
        this.CurrentTime = PZMath.clamp(time, 0.0F, (float)this.MaxTime);
    }

    public void setTime(int maxTime) {
        this.MaxTime = maxTime;
    }

    @Override
    public void OnAnimEvent(AnimEvent event) {
        Object object = this.table.rawget("animEvent");
        if (object != null) {
            LuaManager.caller.pcallvoid(LuaManager.thread, object, this.table, event.m_EventName, event.m_ParameterValue);
        }
    }

    public String getMetaType() {
        return this.table != null && this.table.getMetatable() != null ? this.table.getMetatable().getString("Type") : "";
    }
}
